/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lmodeller.ui.GameButton.TEXTLOCATION;
import glapp.GLApp;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Rectangle;


public class GameListMenu extends UIElement {
	ArrayList<String> list;
	ArrayList<GameTextLabel> labels;
	int firstIndex, selectedIndex;

	GameButton upButton;
	GameButton downButton;
	
	public GameListMenu(Rectangle rectangle, int rowHeight) {
		super(rectangle);
		list = new ArrayList<String>();
		labels = new ArrayList<GameTextLabel>();
		firstIndex = 0;
		selectedIndex = -1;
		int rows = (int)(rectangle.getHeight()*0.8)/rowHeight;
		for(int i=0;i<rows;i++)
		{
			labels.add(new GameTextLabel(new Rectangle(rectangle.getX()+(int)(rectangle.getWidth()*.05), 
					(int)(rectangle.getY() + rectangle.getHeight()*0.95 - (i+1)*rowHeight), 
	    			(int)(rectangle.getWidth()*.8), rowHeight), rowHeight/2, LModeller.actionColor));
			labels.get(i).centerHorizontaly(false);
		}
    	upButton = new GameButton(new Rectangle(rectangle.getX() + (int)(rectangle.getWidth()*.85),
    			(int)(rectangle.getY() + rectangle.getHeight()*.75),
    			(int)(rectangle.getWidth()*.1),
    			(int)(rectangle.getHeight()*.2f)), 
    			GameTextures.getInstance().getTexture("UP_BUTTON"), LModeller.actionColor
    			, null, TEXTLOCATION.UP);
    	downButton = new GameButton(new Rectangle(rectangle.getX() + (int)(rectangle.getWidth()*.85),
    			(int)(rectangle.getY()+rectangle.getHeight()*.05f),
    			(int)(rectangle.getWidth()*.1),
    			(int)(rectangle.getHeight()*.2f)), 
    			GameTextures.getInstance().getTexture("DOWN_BUTTON"), LModeller.actionColor,
    			null, TEXTLOCATION.UP);
	}

	public void paint()
	{
		GL11.glPushMatrix();
    	GL11.glEnable(GL11.GL_TEXTURE_2D);
		upButton.render();
		downButton.render();
		Iterator<GameTextLabel> it = labels.iterator();
		while(it.hasNext())
		{
			it.next().render();
		}
		GLApp.setColor(LModeller.directionColor);
		GLApp.drawRect(this.getBounds().getX(), 
				this.getBounds().getY(), this.getBounds().getWidth(), 
				this.getBounds().getHeight());
        GLApp.setColor(GLApp.colorWhite);
        GL11.glPopMatrix();
	}
	
	public void setMaxFontSize(int fontSize) {
		Iterator<GameTextLabel> it = labels.iterator();
		while(it.hasNext())
		{
			it.next().setMaxFontSize(fontSize);
		}
	}

	public void addFile(String filename) {
		list.add(filename);
		if(list.size()<=labels.size())
		{
			labels.get(list.size()-1).setText(filename);
		}
		setLabels();
	}

	@Override
	public void mouseDown(int x, int y)
	{
		if(contains(x, y) && isVisible)
		{
			clickMenu(x, y);
			ActionEvent action = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand);
			fireActionEvent(action);
		}
	}
	
	public String clickMenu(int x, int y) {
		String itemText = null;
		for(int i=0; i<labels.size();i++)
		{
			GameTextLabel lbl = labels.get(i);
			if(lbl.contains(x, y))
			{
				
				itemText = lbl.getText();
				if(itemText.isEmpty())
				{
					itemText = null;
				}
				else
				{
					selectedIndex = i+firstIndex;
					setSelected();
				}
				break;
			}
		}
		if(downButton.contains(x, y) && list.size()-firstIndex>labels.size())
		{
			firstIndex++;
			setLabels();
		}
		if(upButton.contains(x, y) && firstIndex>0)
		{
			firstIndex--;
			setLabels();
		}
		return itemText;
	}

	public void clear() {
		list.clear();
		firstIndex = 0;
		selectedIndex = -1;
		setLabels();
	}

	protected void setSelected()
	{
		for(int i=0; i<labels.size();i++)
		{
			GameTextLabel lbl = labels.get(i);
			if(i+firstIndex==selectedIndex)
			{
				lbl.setOnOff(true);
			}
			else
			{
				lbl.setOnOff(false);
			}
		}
	}
	
	protected void setLabels()
	{
		for(int i=0; i<labels.size();i++)
		{
			GameTextLabel lbl = labels.get(i);
			if(i+firstIndex<list.size())
			{
				lbl.setText(list.get(i+firstIndex));
			}
			else
			{
				lbl.setText("");
			}
		}
		setSelected();
	}
	
	public int getSelectedIndex()
	{
		return selectedIndex;
	}
	
	public String getSelectedText()
	{
		String selectedText = null;
		if(selectedIndex<list.size() && selectedIndex>=0)
		{
			selectedText = list.get(selectedIndex);
		}
		return selectedText;
	}
	
	public boolean setSelectedIndex(int i)
	{
		if(i<list.size())
		{
			selectedIndex = i;
			setSelected();
			return true;
		}
		else
			return false;
	}

}
